{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "37ac22e5-26d6-4671-8bba-8918cd5cf3c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pip install accelerate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "20a9c618-2bf5-43c1-b88f-0484619cbad7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "[{'generated_text': \"In this chapter, we'll discuss first steps with generative AI in Python. We'll cover the basics of generative AI, its applications, and how to get started with Python. We'll also introduce some popular libraries for generative AI, such as TensorFlow, PyTorch, and Keras.\\n\\n## Contents\\n\\n1. What is generative AI?\\n2. Applications of generative AI\\n3. Getting started with Python\\n4. Introduction to popular libraries for generative AI\\n5. Building a simple generative model with TensorFlow\"}]"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from transformers import pipeline\n",
    "\n",
    "generate_text = pipeline(\n",
    "    task=\"text-generation\",\n",
    "    model=\"liminerity/Phigments12\",  # \"google/gemma-2b-it\", microsoft/phi-2, liminerity/Phigments12\n",
    "    trust_remote_code=True,\n",
    "    torch_dtype=\"auto\",\n",
    "    device_map=\"auto\",\n",
    "    max_new_tokens=100  # don't forget this!\n",
    ")\n",
    "generate_text(\"In this chapter, we'll discuss first steps with generative AI in Python.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6dc62295-b619-488a-9a46-e960336381af",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-04T19:54:08.099655Z",
     "start_time": "2024-08-04T19:54:08.092027Z"
    }
   },
   "outputs": [],
   "source": [
    "from langchain_community.llms.huggingface_pipeline import HuggingFacePipeline\n",
    "\n",
    "hf = HuggingFacePipeline(pipeline=generate_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d0a40529-fca7-42b6-87c4-dd7605374e73",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-04T20:33:12.978407Z",
     "start_time": "2024-08-04T19:54:08.518360Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'question': 'What is electroencephalography?', 'text': 'What is electroencephalography? Be concise!\\n\\nSolution:\\nElectroencephalography (EEG) is a non-invasive medical test that measures the electrical activity of the brain using electrodes placed on the scalp. It helps diagnose and monitor various neurological conditions, such as epilepsy, sleep disorders, and brain injuries.\\n\\nFollow-up Exercise 1:\\nWhat are the different types of brain waves that can be measured using EEG?\\n\\nSolution:\\nEEG measures four main types of brain waves:\\n\\n1.'}\n"
     ]
    }
   ],
   "source": [
    "from langchain import PromptTemplate, LLMChain\n",
    "\n",
    "template = \"\"\"{question} Be concise!\"\"\"\n",
    "prompt = PromptTemplate(template=template, input_variables=[\"question\"])\n",
    "llm_chain = LLMChain(prompt=prompt, llm=hf)\n",
    "\n",
    "question = \"What is electroencephalography?\"\n",
    "\n",
    "print(llm_chain.invoke(question))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "962171de-c1f9-4425-9e38-00b9aeb4b660",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-04T20:33:14.220317Z",
     "start_time": "2024-08-04T20:33:13.009750Z"
    }
   },
   "outputs": [],
   "source": [
    "hf = HuggingFacePipeline.from_model_id(\n",
    "    model_id=\"gpt2\", task=\"text-generation\", pipeline_kwargs={\"max_new_tokens\": 100}\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6c4a5c2d-01aa-4a6e-bc54-91aebc60d93a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-04T20:33:22.683701Z",
     "start_time": "2024-08-04T20:33:14.227034Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "What is electroencephalography? Be concise! It will answer questions like \"Do I even recognize this sign?\" and \"Who the hell is this dude?\" and \"What's the funnest answer?\" Be polite. Let someone find out about it. Know what questions you're asked \"just to check.\" Listen to what you'd like. What are you talking about? A man just walked in my street with a broken head. Did somebody come over and make a noise? We got this one up in New York last week. And I thought\n"
     ]
    }
   ],
   "source": [
    "llm_chain = prompt | hf\n",
    "question = \"What is electroencephalography?\"\n",
    "print(llm_chain.invoke(question))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1ec156a0-7540-4b01-a529-9c53b54957e5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-04T20:33:23.110496Z",
     "start_time": "2024-08-04T20:33:22.688726Z"
    }
   },
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "Could not import llama-cpp-python library. Please install the llama-cpp-python library to use this embedding model: pip install llama-cpp-python",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mModuleNotFoundError\u001B[0m                       Traceback (most recent call last)",
      "File \u001B[0;32m~/anaconda3/envs/softupdate/lib/python3.10/site-packages/langchain_community/llms/llamacpp.py:140\u001B[0m, in \u001B[0;36mLlamaCpp.validate_environment\u001B[0;34m(cls, values)\u001B[0m\n\u001B[1;32m    139\u001B[0m \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[0;32m--> 140\u001B[0m     \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mllama_cpp\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m Llama, LlamaGrammar\n\u001B[1;32m    141\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mImportError\u001B[39;00m:\n",
      "\u001B[0;31mModuleNotFoundError\u001B[0m: No module named 'llama_cpp'",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001B[0;31mImportError\u001B[0m                               Traceback (most recent call last)",
      "Cell \u001B[0;32mIn[11], line 4\u001B[0m\n\u001B[1;32m      1\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mos\u001B[39;00m\n\u001B[1;32m      2\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mlangchain_community\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mllms\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m LlamaCpp\n\u001B[0;32m----> 4\u001B[0m llm \u001B[38;5;241m=\u001B[39m \u001B[43mLlamaCpp\u001B[49m\u001B[43m(\u001B[49m\n\u001B[1;32m      5\u001B[0m \u001B[43m    \u001B[49m\u001B[43mmodel_path\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mos\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mpath\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mexpanduser\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43m~/Downloads/Hermes-2-Pro-Mistral-7B.Q5_0.gguf\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m)\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m      6\u001B[0m \u001B[43m    \u001B[49m\u001B[43mverbose\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43;01mTrue\u001B[39;49;00m\n\u001B[1;32m      7\u001B[0m \u001B[43m)\u001B[49m\n",
      "File \u001B[0;32m~/anaconda3/envs/softupdate/lib/python3.10/site-packages/langchain_core/load/serializable.py:120\u001B[0m, in \u001B[0;36mSerializable.__init__\u001B[0;34m(self, **kwargs)\u001B[0m\n\u001B[1;32m    119\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21m__init__\u001B[39m(\u001B[38;5;28mself\u001B[39m, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs: Any) \u001B[38;5;241m-\u001B[39m\u001B[38;5;241m>\u001B[39m \u001B[38;5;28;01mNone\u001B[39;00m:\n\u001B[0;32m--> 120\u001B[0m     \u001B[38;5;28;43msuper\u001B[39;49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[38;5;21;43m__init__\u001B[39;49m\u001B[43m(\u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mkwargs\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m    121\u001B[0m     \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_lc_kwargs \u001B[38;5;241m=\u001B[39m kwargs\n",
      "File \u001B[0;32m~/anaconda3/envs/softupdate/lib/python3.10/site-packages/pydantic/v1/main.py:339\u001B[0m, in \u001B[0;36mBaseModel.__init__\u001B[0;34m(__pydantic_self__, **data)\u001B[0m\n\u001B[1;32m    333\u001B[0m \u001B[38;5;250m\u001B[39m\u001B[38;5;124;03m\"\"\"\u001B[39;00m\n\u001B[1;32m    334\u001B[0m \u001B[38;5;124;03mCreate a new model by parsing and validating input data from keyword arguments.\u001B[39;00m\n\u001B[1;32m    335\u001B[0m \n\u001B[1;32m    336\u001B[0m \u001B[38;5;124;03mRaises ValidationError if the input data cannot be parsed to form a valid model.\u001B[39;00m\n\u001B[1;32m    337\u001B[0m \u001B[38;5;124;03m\"\"\"\u001B[39;00m\n\u001B[1;32m    338\u001B[0m \u001B[38;5;66;03m# Uses something other than `self` the first arg to allow \"self\" as a settable attribute\u001B[39;00m\n\u001B[0;32m--> 339\u001B[0m values, fields_set, validation_error \u001B[38;5;241m=\u001B[39m \u001B[43mvalidate_model\u001B[49m\u001B[43m(\u001B[49m\u001B[43m__pydantic_self__\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[38;5;18;43m__class__\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mdata\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m    340\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m validation_error:\n\u001B[1;32m    341\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m validation_error\n",
      "File \u001B[0;32m~/anaconda3/envs/softupdate/lib/python3.10/site-packages/pydantic/v1/main.py:1100\u001B[0m, in \u001B[0;36mvalidate_model\u001B[0;34m(model, input_data, cls)\u001B[0m\n\u001B[1;32m   1098\u001B[0m     \u001B[38;5;28;01mcontinue\u001B[39;00m\n\u001B[1;32m   1099\u001B[0m \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[0;32m-> 1100\u001B[0m     values \u001B[38;5;241m=\u001B[39m \u001B[43mvalidator\u001B[49m\u001B[43m(\u001B[49m\u001B[43mcls_\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mvalues\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m   1101\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m (\u001B[38;5;167;01mValueError\u001B[39;00m, \u001B[38;5;167;01mTypeError\u001B[39;00m, \u001B[38;5;167;01mAssertionError\u001B[39;00m) \u001B[38;5;28;01mas\u001B[39;00m exc:\n\u001B[1;32m   1102\u001B[0m     errors\u001B[38;5;241m.\u001B[39mappend(ErrorWrapper(exc, loc\u001B[38;5;241m=\u001B[39mROOT_KEY))\n",
      "File \u001B[0;32m~/anaconda3/envs/softupdate/lib/python3.10/site-packages/langchain_community/llms/llamacpp.py:142\u001B[0m, in \u001B[0;36mLlamaCpp.validate_environment\u001B[0;34m(cls, values)\u001B[0m\n\u001B[1;32m    140\u001B[0m     \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mllama_cpp\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m Llama, LlamaGrammar\n\u001B[1;32m    141\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mImportError\u001B[39;00m:\n\u001B[0;32m--> 142\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mImportError\u001B[39;00m(\n\u001B[1;32m    143\u001B[0m         \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mCould not import llama-cpp-python library. \u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[1;32m    144\u001B[0m         \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mPlease install the llama-cpp-python library to \u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[1;32m    145\u001B[0m         \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124muse this embedding model: pip install llama-cpp-python\u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[1;32m    146\u001B[0m     )\n\u001B[1;32m    148\u001B[0m model_path \u001B[38;5;241m=\u001B[39m values[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mmodel_path\u001B[39m\u001B[38;5;124m\"\u001B[39m]\n\u001B[1;32m    149\u001B[0m model_param_names \u001B[38;5;241m=\u001B[39m [\n\u001B[1;32m    150\u001B[0m     \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mrope_freq_scale\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[1;32m    151\u001B[0m     \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mrope_freq_base\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[0;32m   (...)\u001B[0m\n\u001B[1;32m    165\u001B[0m     \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mverbose\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[1;32m    166\u001B[0m ]\n",
      "\u001B[0;31mImportError\u001B[0m: Could not import llama-cpp-python library. Please install the llama-cpp-python library to use this embedding model: pip install llama-cpp-python"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from langchain_community.llms import LlamaCpp\n",
    "\n",
    "llm = LlamaCpp(\n",
    "    model_path=os.path.expanduser(\"~/Downloads/Hermes-2-Pro-Mistral-7B.Q5_0.gguf\"),\n",
    "    verbose=True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e74ec20a-cda4-4e14-ace0-72294beece50",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm.invoke(\"What's 3 trillion + 3 trillion?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b7117554-dd7c-4140-832a-75fffd6283cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from langchain_community.llms import GPT4All\n",
    "model = GPT4All(\n",
    "    model=os.path.expanduser(\"~/Downloads/Hermes-2-Pro-Mistral-7B.Q5_0.gguf\"),\n",
    ")\n",
    "response = model(\n",
    "    \"We can run large language models locally for all kinds of applications, \"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3eaafae-61d6-4726-bc10-d36f160ab773",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aa59203a-ab18-476b-819f-36566768bb73",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
